package com.pimee.service.admin.impl;

import com.pimee.common.exception.BusinessException;
import com.pimee.common.util.DateUtil;
import com.pimee.common.util.StringUtils;
import com.pimee.mapper.SysUserMapper;
import com.pimee.mapper.ext.ExtSysDeptMapper;
import com.pimee.model.SysDept;
import com.pimee.model.SysUser;
import com.pimee.model.web.Ztree;
import com.pimee.service.BaseService;
import com.pimee.service.admin.ISysDeptService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysDeptService extends BaseService<SysDept> implements ISysDeptService {

	@Resource
	private ExtSysDeptMapper extSysDeptMapper;
	@Resource
	private SysUserMapper sysUserMapper;
	
	@Override
	public List<Ztree> treeDept() {
		List<SysDept> deptList = this.listAll();
        List<Ztree> ztrees = initZtree(deptList);
        return ztrees;
	}
	
	/**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SysDept> deptList) {
        return initZtree(deptList, null);
    }

    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @param roleDeptList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SysDept> deptList, List<String> roleDeptList) {
        List<Ztree> ztrees = new ArrayList<>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (SysDept dept : deptList) {
        	if("0".equals(dept.getStatus()) && "0".equals(dept.getDelFlag())) {
	            Ztree ztree = new Ztree();
	            ztree.setId(dept.getDeptId());
	            ztree.setpId(dept.getParentId());
	            ztree.setName(dept.getDeptName());
	            ztree.setTitle(dept.getDeptName());
	            if (isCheck){
	                ztree.setChecked(roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
	            }
	            ztrees.add(ztree);
        	}
        }
        return ztrees;
    }

	@Override
	public SysDept selectDeptById(Long deptId) {
		return this.selectByKey(deptId);
	}

	@Override
	public List<SysDept> listDept(Map<String, Object> params) {
		return extSysDeptMapper.listDept(params);
	}

	@Override
	public void insertDept(SysDept param) {
		Date now = DateUtil.now();
        param.setCreateTime(now);
        param.setUpdateTime(now);
		this.saveNotNull(param);
	}

	@Override
	public void updateDept(SysDept param) {
        param.setUpdateTime(DateUtil.now());
		this.updateNotNull(param);
	}

	@Override
	public void deleteDeptById(Long deptId) {
		Example example = new Example(SysDept.class);
    	example.createCriteria().andEqualTo("parentId", deptId);
        int deptCount = this.countByExample(example);
		if (deptCount > 0) {
            throw new BusinessException("存在下级部门, 不允许删除");
        }
		
		Example userExample = new Example(SysUser.class);
		userExample.createCriteria().andEqualTo("deptId", deptId);
        int userCount = sysUserMapper.selectCountByExample(userExample);
        if (userCount > 0) {
            throw new BusinessException("部门存在用户, 不允许删除");
        }
        
        this.deleteByKey(deptId);
	}
}
